<style>
  .picImg {
    width: 40px;
    height: 40px;
  }

  .input-unit {
    position: relative;
    top: -32px;
    left: 304px;
  }
  .el-dialog {
    width: 1100px;
  }
</style>
<template>
  <div class="app-container">
    <!--&lt;!&ndash; 查询和其他操作 &ndash;&gt;-->
    <div class="filter-container">

      <el-button class="filter-item" type="primary" icon="el-icon-edit"
                 @click="handleAdd">新增
      </el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row
              :header-cell-style="{background:'#92D3FF',color:'#fff'}">

      <el-table-column fixed align="center" min-width="100" label="渠道编号" prop="channelNo"/>
      <el-table-column fixed align="center" min-width="100" label="渠道名称" prop="channelName"/>
      <el-table-column align="center" min-width="150" label="渠道服务URL地址" prop="serverURL"/>
      <el-table-column align="center" min-width="100" label="类型" prop="type">
        <template slot-scope="scope">
          <span>{{ scope.row.type|typeFilter}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" min-width="150" label="开户银行名称" prop="interBankName"/>
      <!--<el-table-column align="center" min-width="100" label="银行名称" prop="bankName"/>-->
      <el-table-column align="center" min-width="150" label="每秒最大并发提交数" prop="PTS"/>
      <el-table-column align="center" min-width="100" label="开放类型" prop="openType">
        <template slot-scope="scope">
          <el-tag>{{{"0":"全年","1":"公众日","2":"周末"}[scope.row.openType]}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" min-width="100" label="开始时间" prop="startTime"/>
      <el-table-column align="center" min-width="100" label="结束时间" prop="endTime"/>
      <el-table-column align="center" min-width="100" label="渠道优先级" prop="level"/>
      <el-table-column align="center" min-width="150" label="费用计算方式" prop="calcType">
        <template slot-scope="scope">
          <el-tag>{{{"0":"四舍五入","1":"向下取整","2":"向上取整"}[scope.row.calcType]}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" min-width="100" label="渠道类型" prop="channelType">
        <template slot-scope="scope">
          <el-tag>{{{"0":"代付渠道","1":"专用渠道"}[scope.row.channelType]}}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" min-width="100" label="状态" prop="status">
        <template slot-scope="scope">
          <el-tag>{{{"0":"正常","1":"禁用"}[scope.row.status]}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" min-width="100" label="备注" prop="remark"/>
      <el-table-column fixed="right" align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini"
                     @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-button type="primary" size="mini"
                     @click="handleForward(scope.row)">费用列表
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
                @pagination="getList"/>
    <!-- 分配订单对话框 -->
    <el-dialog :visible.sync="editDialogVisible" :title="textMap[dialogStatus]" :close-on-click-modal="false">
      <el-form ref="editForm" :rules="editFormRules" :model="editForm" status-icon label-position="left"
               label-width="100px" style="width: 900px; margin-left:50px;display: flex">
        <div style="flex: 1;margin-right: 40px">
          <el-form-item label="渠道编号" prop="channelNo">
            <el-input :disabled="dialogStatus=='update'" v-model="editForm.channelNo" placeholder="渠道编号"/>
          </el-form-item>
          <el-form-item label="渠道名称" prop="channelName">
            <el-input v-model="editForm.channelName" auto-complete="off" placeholder="渠道名称"/>
          </el-form-item>
          <el-form-item label="渠道服务URL地址" prop="serverURL">
            <el-input v-model="editForm.serverURL" auto-complete="off" placeholder="渠道服务URL地址"/>
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-radio-group v-model="editForm.type">
              <el-radio :label="0">银企直连</el-radio>
              <el-radio :label="1">第三方机构</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="银行名称" prop="bankName" v-if="editForm.type===0">
            <el-select @change="bankNameChange" v-model="editForm.bankName" placeholder="请选择" filterable>
              <el-option
                v-for="item in banks"
                :key="item.bankName"
                :label="item.bankName"
                :value="item.bankCode"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="开户银行名称" prop="interBankName" v-if="editForm.type===0">
            <el-input readonly="readonly" v-model="editForm.interBankName" auto-complete="off" placeholder="请选择" @click.native="getUnioLs()"/>
              <!--<el-select v-model="editForm.interBankNo" placeholder="请选择开户银行名称" filterable>
              <el-option
                v-for="item in unionnos"
                :key="item.bankName"
                :label="item.bankName"
                :value="item.interBankNo"
              >
              </el-option>
            </el-select>-->
          </el-form-item>
          <el-form-item label="每秒最大请求并发数" prop="PTS">
            <el-input v-model="editForm.PTS" auto-complete="off" placeholder="每秒最大请求并发数"/>
          </el-form-item>
          <el-form-item label="业务类型">
            <el-radio-group v-model="editForm.businessType">
              <el-radio :label="1">代收</el-radio>
              <el-radio :label="2">代付</el-radio>
              <el-radio :label="3">代收+代付</el-radio>
            </el-radio-group>

          </el-form-item>
          <el-form-item label="业务子类型" prop="subBusiness">
            <el-radio-group v-model="editForm.subBusiness">
              <el-radio :label="1">对公</el-radio>
              <el-radio :label="2">对私</el-radio>
              <el-radio :label="3">对公+对私</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="开放类型" prop="openType">
            <el-radio-group v-model="editForm.openType">
              <el-radio :label="0">全年</el-radio>
              <el-radio :label="1">工作日</el-radio>
              <el-radio :label="2">周末</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="开始时间">
            <el-time-picker
              v-model="editForm.startTime"
              :picker-options="{
                selectableRange: '00:00:00 - 23:59:59'
              }"
              value-format="HH:mm:ss"
              placeholder="开始时间">
            </el-time-picker>
          </el-form-item>
          <el-form-item label="结束时间">
            <el-time-picker
              v-model="editForm.endTime"
              :picker-options="{
                selectableRange: '00:00:00 - 23:59:59'
              }"
              value-format="HH:mm:ss"
              placeholder="结束时间">
            </el-time-picker>
          </el-form-item>
          <el-form-item label="交易最小限额" prop="minMoney">
            <el-input v-model="editForm.minMoney" auto-complete="off" placeholder="交易最小限额"/>
            <span class="input-unit">元</span>
          </el-form-item>
        </div>
        <div style="flex: 1">
          <el-form-item label="交易最大限额" prop="maxMoney">
            <el-input v-model="editForm.maxMoney" auto-complete="off" placeholder="交易最大限额"/>
            <span class="input-unit">元</span>
          </el-form-item>
          <el-form-item label="当日累计最大交易金额" prop="dayMaxMoney">
            <el-input v-model="editForm.dayMaxMoney" auto-complete="off" placeholder="当日累计最大交易金额"/>
            <span class="input-unit">元</span>
          </el-form-item>
          <el-form-item label="当日累计最大交易笔数" prop="dayMaxTimes">
            <el-input v-model="editForm.dayMaxTimes" auto-complete="off" placeholder="当日累计最大交易笔数"/>
          </el-form-item>
          <el-form-item label="渠道优先级" prop="level">
            <el-input v-model="editForm.level" auto-complete="off" placeholder="渠道优先级"/>
          </el-form-item>
          <el-form-item label="费用计算方式" prop="calcType">
            <el-radio-group v-model="editForm.calcType">
              <el-radio :label="0">四舍五入</el-radio>
              <el-radio :label="1">向下取整</el-radio>
              <el-radio :label="2">向上取整</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="渠道类型" prop="channelType">
            <el-radio-group v-model="editForm.channelType">
              <el-radio :label="0">代付渠道</el-radio>
              <el-radio :label="1">专用渠道</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="editForm.status">
              <el-radio :label="0">正常</el-radio>
              <el-radio :label="1">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="访问代码" prop="code">
            <el-input v-model="editForm.code" auto-complete="off" placeholder="访问代码"/>
          </el-form-item>
          <el-form-item label="访问秘钥" prop="secrit">
            <el-input v-model="editForm.secrit" auto-complete="off" placeholder="访问秘钥"/>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="editForm.remark" auto-complete="off" placeholder="备注"/>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="doCreate">确定</el-button>
        <el-button v-else type="primary" @click="doEdit">确定</el-button>
      </div>
    </el-dialog>

    <!--支行弹窗-->
    <el-dialog :visible.sync="UnionDialogVisible" title="选择开户银行名称" :close-on-click-modal="false">
      <div class="filter-container">
        <el-form :inline="true" status-icon label-position="left" class="demo-form-inline"
                 label-width="100px" style="width: 100%; margin-left:50px;">
          <el-form-item label="开户银行名称" prop="interBankName">
            <el-input auto-complete="off" v-model="editForm1.interBank" placeholder="开户银行名称" style="width:400px" />
          </el-form-item>
          <el-form-item>
            <el-button class="filter-item" type="primary" @click="queryUnio">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table v-loading="unionnosLoading" :data="unionnos" element-loading-text="正在查询中。。。" border fit highlight-current-row
                :header-cell-style="{background:'#92D3FF',color:'#fff'}">
        <el-table-column align="left" min-width="100" label="开户银行名称" prop="bankName"/>
        <el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="primary" size="mini"
                       @click="choose(scope.row)">选择
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total1>0" :total="total1" :page.sync="editForm1.pageNum" :limit.sync="editForm1.pageSize"
                  @pagination="getUnionnos"/>
    </el-dialog>

  </div>
</template>

<script>
  import { doGet, doPost } from '@/api'
  import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
  import checkPermission from '@/utils/permission' // 权限判断函数
  export default {
    name: 'bank',
      al : '',
      pageNum: '',
      pageSize: '',
    components: { Pagination },
    filters: {
      typeFilter(type) {
        return { '0': '银企直连', '1': '第三方机构' }[type]
      }

    },
    data() {
      return {
        pickerOptions1: {
          disabledDate(time) {
            return time.getTime() > Date.now()
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date())
            }
          }]
        },
        textMap: {
          update: '编辑',
          create: '创建'
        },
        dialogStatus: undefined,
        editDialogVisible: false,
        list: [],
        banks:[],
        unionnos:[],
        total: 0,
        total1: 0,
        listLoading: true,
        listQuery: {
          pageNum: 1,
          pageSize: 20
        },
        editForm: {
          channelNo: undefined,
          channelName: undefined,
          bankName: undefined,
          serverURL: undefined,
          type: undefined,
          PTS: undefined,
          businessType: undefined,
          subBusiness: undefined,
          openType: undefined,
          startTime: '',
          endTime: '',
          minMoney: undefined,
          maxMoney: undefined,
          dayMaxMoney: undefined,
          dayMaxTimes: undefined,
          level: undefined,
          calcType: undefined,
          channelType: undefined,
          status: undefined,
          code: undefined,
          secrit: undefined,
          remark: undefined,
            interBankNo: undefined,
            bankCode: undefined,
           interBankName: undefined
        },
        editFormRules: {
          channelNo: [{ required: true, message: '渠道编号不允许为空', trigger: 'blur' }],
          channelName: [{ required: true, message: '渠道名称不允许为空', trigger: 'blur' }],
          serverURL: [{ required: true, message: '渠道服务URL地址不允许为空', trigger: 'blur' }],
            bankCode: [{ required: true, message: '银行名称不允许为空', trigger: 'blur' }],
            interBankNo: [{ required: true, message: '开户银行名称不允许为空', trigger: 'blur' }],
          startTime: [{ required: true, message: '开始时间不允许为空', trigger: 'blur' }],
          endTime: [{ required: true, message: '结束时间不允许为空', trigger: 'blur' }],
          level: [{ required: true, message: '优先级不允许为空', trigger: 'blur' }],
          calcType: [{ required: true, message: '计费方式不允许为空', trigger: 'blur' }],
          channelType: [{ required: true, message: '渠道类型不允许为空', trigger: 'blur' }],
          status: [{ required: true, message: '渠道状态不允许为空', trigger: 'blur' }]
        },
          editForm1: {
              pageNum: 1,
              pageSize: 20,
              bankCode: undefined,
              interBank: undefined
          },
          UnionDialogVisible: false,
          unionnosLoading: true,

      }
    },
    created() {
      this.getList()
      this.getBanks()
    },
    methods: {
      checkPermission,
      getBanks() {
          doGet('bank.html?op=list', {}).then(response => {
              this.banks = response.rows
          }).catch(() => {
              this.banks = []
          })
      },
        getUnionnos(subankCode, flag) {
            if (typeof(subankCode) == 'string') {
                this.pageNum = 1
                this.pageSize = 20
            } else {
                this.pageNum = subankCode.page
                this.pageSize = subankCode.limit
            }
            this.editForm1 = {
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                bankCode: this.al,
                bankName: this.editForm1.interBank,
                interBankNo: this.editForm.interBankNo,
                flag: flag
                /*bankName: this.editForm.interBankNo*/
            }
            this.UnionDialogVisible = true
            this.unionnosLoading = true
            doGet('unionno.html?op=list', this.editForm1).then(response => {
                this.unionnos = response.rows
                this.total1 = response.data.totalcount
                this.unionnosLoading = false
            }).catch(() => {
                this.unionnos = []
                this.total1 = 0
                this.unionnosLoading = false
            })
        },
      bankNameChange(bankCode) {
          this.al = bankCode.substring(0, 3)
           var flag = '1';
          this.getUnionnos(this.al, flag)
        this.editForm.interBankNo = undefined
      },
      getList() {
        this.listLoading = true
        doGet('channel.html?op=channellist', this.listQuery).then(response => {
          this.list = response.rows
          this.total = response.data.totalcount
          this.listLoading = false
        }).catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
      },
      handleAdd() {
        this.editForm = {
          channelNo: undefined,
          channelName: undefined,
          bankName: undefined,
          serverURL: undefined,
          type: 0,
          bankCode: undefined,
          PTS: undefined,
          businessType: 1,
          subBusiness: 1,
          openType: 1,
          startTime: '00:00:01',
          endTime: '23:59:59',
          minMoney: undefined,
          maxMoney: undefined,
          dayMaxMoney: undefined,
          dayMaxTimes: undefined,
          level: undefined,
          interBankNo: this.unionnos.interBankNo,
          calcType: 1,
          channelType: 1,
          status: 0,
          code: undefined,
          secrit: undefined,
          remark: undefined,
        }
        this.dialogStatus = 'create'
        this.editDialogVisible = true
        this.$nextTick(() => {
          this.$refs['editForm'].clearValidate()
        })
      },

      doCreate() {
        this.$refs['editForm'].validate(valid => {
          if (valid) {
            doGet('channel.html?op=addchannel', this.editForm)
              .then(response => {
                this.getList()
                this.editDialogVisible = false
                this.$notify.success({
                  title: '成功',
                  message: '添加成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.message
                })
              })
          }
        })
      },
      handleEdit(row) {
        this.editForm = Object.assign({}, row)
        this.editIndex = this.list.indexOf(row)
        this.dialogStatus = 'update'
        this.editDialogVisible = true
        this.$nextTick(() => {
          this.$refs['editForm'].clearValidate()
        })
      },
      doEdit() {
        this.$refs['editForm'].validate(valid => {
          if (valid) {
            doGet('channel.html?op=chgchannel', this.editForm)
              .then(response => {
                this.editDialogVisible = false
                this.list[this.editIndex] = this.editForm
                this.$notify.success({
                  title: '成功',
                  message: '修改成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.message
                })
              })
          }
        })
      },
      /**跳转渠道费用列表 */
      handleForward(row) {
        this.$router.push({ path: '/channelAcc', query: { channelNo: row.channelNo }})
      },
        /**查询银行名称*/
        queryUnio() {
            this.UnionDialogVisible = true
            this.pageNum = this.pageNum
            this.pageSize = this.pageSize
            this.getUnionnos(this.al, '2')
        },
         getUnioLs() {
        if(this.al == null){
            alert('请选择银行名称')
          } else {
            this.UnionDialogVisible = true
            this.pageNum = this.pageNum
            this.pageSize = this.pageSize
            this.getUnionnos(this.al, '1')
          }
        },
        /** 选择分行*/
        choose(row) {
            this.editForm.interBankNo = row.interBankNo
            this.editForm.interBankName = row.bankName
            this.editForm1.interBan = row.bankName
            this.UnionDialogVisible = false
        },

    }
  }
</script>
